<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>状态</p>
          <el-select v-model="selectedMsg.IsEnable" size="mini" placeholder="请选择卡种状态" filterable @change="getPageList()">
            <el-option v-for="item in cardStatus" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :span="20" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="gl_mb_15">
      <el-button type="primary" size="mini" @click="clickAdd">新建卡种</el-button>
    </div>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column prop="name" label="卡种名称" />
      <el-table-column label="计费方式">
        <template slot-scope="{row}">
          <span>{{ chooseChargeType(row.chargeTypeCode) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="卡内课时">
        <template slot-scope="{row}">
          <span v-if="row.chargeTypeCode === 'Date'">无限</span>
          <span v-else>{{ row.classPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="effectivePeriod" label="默认有效期">
        <template slot-scope="scope">
          {{ scope.row.effectivePeriod + ' ' + scope.row.effectivePeriodType.name }}
        </template>
      </el-table-column>
      <el-table-column label="售价">
        <template slot-scope="{row}">
          {{ (row.price/100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="创建人">
        <template slot-scope="{row}">
          {{ row.createAccount }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{row}">
          <div v-if="row.isEnable"><i class="success_circle" />已启用</div>
          <div v-else><i class="error_circle" />已停用</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="{ row }">
          <el-button v-if="row.isEnable" size="mini" type="text" @click="clickEnabledState(row, false)">停用</el-button>
          <el-button v-else size="mini" type="text" @click="clickEnabledState(row, true)">启用</el-button>
          <el-button size="mini" type="text" @click="clickEdit(row)">编辑</el-button>
          <el-button size="mini" type="text" @click="clickDelete(row)"><span class="gl_color_error">删除</span></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="selectedMsg.MaxResultCount"
        :current-page="selectedMsg.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
    <add-card ref="addCard" />
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import { deepCopy } from '@/lib';
import addCard from './addCard.vue';
import { Api_GET_ClubMembershipCardsQO } from '@/lib/form/hall/swaggerForm';
import { api_GET_ClubMembershipCards, api_DELETE_ClubMembershipCardsId, api_PUT_ClubMembershipCardsIdStatus } from '@/extend/api/hallswaggerApi/app';
export default {
  components: { addCard },
  data () {
    return {
      selectedMsg: new Api_GET_ClubMembershipCardsQO(),
      cardStatus: [{ code: null, value: '全部状态' }, { code: false, value: '已停用' }, { code: true, value: '已启用' }],
      tableData: [],
      loading: false,
      total: 0
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache
    })
  },
  mounted() {
    if (this.selectedMsg) {
      this.selectedMsg.IsEnable = null;
    }
    this.getPageList();
    this.getDict();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    getPageList(page) {
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.loading = true;
      api_GET_ClubMembershipCards(this.selectedMsg).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickAdd() {
      this.$refs.addCard.dialogVisible = true;
      this.$refs.addCard.title = '新建卡种';
      this.$refs.addCard.cardForm.isEnable = true;
    },
    // 启用停用
    clickEnabledState(row, status) {
      const that = this;
      const content = status ? '启用' : '停用';
      that.$confirm(`您确定要` + content + `吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true;
          api_PUT_ClubMembershipCardsIdStatus(row.id, { isEnable: status }).then(res => {
            this.loading = false;
            this.$message({ type: 'success', message: '修改成功' });
            this.getPageList();
          }, error => {
            this.loading = false;
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    // 编辑卡种
    clickEdit(row) {
      const price = Number(row.price);
      this.$refs.addCard.cardForm = deepCopy(row);
      this.$refs.addCard.cardForm.price = Number((price / 100).toFixed(2));
      this.$refs.addCard.currentId = row.id;
      this.$refs.addCard.changeit();
      this.$refs.addCard.dialogVisible = true;
      this.$refs.addCard.title = '编辑卡种';
    },
    // 删除
    clickDelete (row) {
      const that = this;
      that.$confirm(`您确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          that.loading = true;
          api_DELETE_ClubMembershipCardsId(row.id).then(res => {
            if (res) {
              that.loading = false;
              this.getPageList();
              this.$message({ type: 'success', message: '删除成功' });
            }
          }, error => {
            that.loading = false;
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    // 操作chargeTypeCode
    chooseChargeType(chargeTypeCode) {
      let _chargeType;
      if (chargeTypeCode && this.dict && this.dict.chargeType) {
        _chargeType = this.dict.chargeType.find(item => item.code === chargeTypeCode);
      }
      _chargeType = _chargeType ? _chargeType.value : '';
      return `${_chargeType}`;
    },
    // 重置
    clickReset() {
      this.selectedMsg.IsEnable = null;
      this.getPageList();
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.selectedMsg.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>

<style lang="scss" scoped>
.success_circle {
  display: inline-block;
  margin-right: 8px;
  width: 6px;
  height: 6px;
  background: #14CE67;
  border-radius: 50%;
}
.error_circle {
  display: inline-block;
  margin-right: 8px;
  width: 6px;
  height: 6px;
  background: #FF3434;
  border-radius: 50%;
}
</style>
